package net.esj.basic.core.engine.tools.crypt.inst.impl;

import net.esj.basic.core.engine.tools.crypt.AESCryptImpl;
import net.esj.basic.core.engine.tools.crypt.Crypt;
import net.esj.basic.core.engine.tools.crypt.MD5CryptImpl;
import net.esj.basic.core.engine.tools.crypt.inst.PwdEncryptor;

public class TwiceMD5WithAESPwdEncryptor implements PwdEncryptor {

	private Crypt md5 = new MD5CryptImpl();
	private Crypt rsa = new AESCryptImpl();
	
	private String secretKey = null;
	
	@Override
	public boolean checksum(String pwd, String checkcode, String result) {
		String str = encrypt(pwd, checkcode);
		if(str.equals(result)){
			return true;
		}
		return false;
	}

	@Override
	public String encrypt(String pwd, String checkcode) {
		String first = md5.encrypt(pwd);
		String second = "";
		if(secretKey!=null){
			second = rsa.encrypt(first,secretKey.getBytes());
		}else{
			second =  rsa.encrypt(first);
		}
		return md5.encrypt(second+checkcode);
	}

	@Override
	public void setSecretKey(String key) {
		this.secretKey = key;
	}

	public static void main(String[] args){
		TwiceMD5WithAESPwdEncryptor encr = new TwiceMD5WithAESPwdEncryptor();
		System.out.println(encr.encrypt("123456", "admin"));
		
	}
}
